package com.mtw.bbs.fileAdmin.util;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 图片压缩工具
 */
@Slf4j
@Component
public class ImgCompressUtil {

    @Value("tempFilePath")
    String tempFilePath;

    /**
     *
     * @param file                      spring文件
     * @param desFileSize               指定图片大小，单位kb
     * @param fileName                  临时文件名
     * @param accuracy                  精度，递归压缩的比率，建议小于0.9
     * @throws IOException
     */
    public File compressPicture(MultipartFile file , Long desFileSize, String fileName,
                                double accuracy) throws IOException {


        File imgFile = this.craeteFile(fileName);
        // 默认500kb
        long defaultSize = 500*1024;
        if (desFileSize != null){
            defaultSize = desFileSize*1024;
        }
        //计算宽高
        BufferedImage bim = ImageIO.read(file.getInputStream());
        // 原宽高
        int imgWidth = bim.getWidth();
        int imgHeight = bim.getHeight();
        // 压缩后宽高
        int desWidth = new BigDecimal(imgWidth).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(imgHeight).multiply(
                new BigDecimal(accuracy)).intValue();
        // 开始压缩
        Thumbnails.of(file.getInputStream()).size(desWidth, desHeight).outputQuality(accuracy).toFile(imgFile);
        //如果不满足要求,递归直至满足小于500k的要求
        if(imgFile.length() <= defaultSize){
            return imgFile;
        }
        return compressPicture(imgFile, defaultSize, accuracy);
    }



    /**
     *
     * @param file              文件
     * @param desFileSize       指定图片大小，单位kb
     * @param accuracy          精度，递归压缩的比率，建议小于0.9
     */
    public File compressPicture(File file , Long desFileSize, double accuracy) throws IOException {

        // 默认500kb
        long defaultSize = 500*1024;
        if (desFileSize != null){
            defaultSize = desFileSize*1024;
        }
        //计算宽高
        BufferedImage bim = ImageIO.read(file);
        // 原宽高
        int imgWidth = bim.getWidth();
        int imgHeight = bim.getHeight();
        // 压缩后宽高
        int desWidth = new BigDecimal(imgWidth).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(imgHeight).multiply(
                new BigDecimal(accuracy)).intValue();
        // 开始压缩
        Thumbnails.of(file).size(desWidth, desHeight).outputQuality(accuracy).toFile(file);
        //如果不满足要求,递归直至满足小于500k的要求
        if(file.length() <= defaultSize){
            return file;
        }
        return compressPicture(file, defaultSize, accuracy);
    }


    /**
     * 创建临时文件
     * @param fileName
     * @return
     */
    private File craeteFile(String fileName){

        File dir = new File(tempFilePath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        return new File(tempFilePath+ File.separator+fileName);
    }


}
